package com.weibo.dip.hatest;

import java.util.concurrent.TimeUnit;

/** Created by haisen on 2018/7/31. */
public class ProgramControl {

  private static boolean running = false;

  private static final String startcommand =
      "java -cp /usr/home/shixi_dongxue3/other/other/target/other-1.0.jar:/usr/home/shixi_dongxue3/other/other/target/lib/* com.weibo.dip.hatest.Testonlyloop";

  private static Runtime runtime = Runtime.getRuntime();

  private static Process process = null;

  /** start jar. */
  public static void start() {
    /*if (!running) {
      System.out.println("Start");
      running = true;
    }*/
    if (!running) {
      try {
        System.out.println("Exe start");
        process = runtime.exec(startcommand);
        System.out.println("Start End");
        running = true;
      } catch (Exception e) {
        System.out.println("Get error");
      }
    }
  }

  /** stop jar. */
  public static void stop() {
   /* if (running) {
      System.out.println("Stop");
      running = false;
    }*/
     if (running && process != null) {
      System.out.println("Exe destory");
      process.destroy();
      try {
        System.out.println("Exe waitFor");
        process.waitFor();
        System.out.println("End waitFor");
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("Stop End");
      running = false;
    }
  }

  public static void main(String[] args) {
    System.out.println("Ready start....");
    ProgramControl.start();
    System.out.println("Start sleep 10000");
    try {
      TimeUnit.MILLISECONDS.sleep(5000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("Ready stop ...");
    ProgramControl.stop();
    System.out.println("Stop");
  }
}
